package com.link.base.base.dutyallurls.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.link.core.basic.query.Filter;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.base.base.dutyallurls.model.DutyAllUrls;
import com.link.base.base.dutyallurls.service.DutyAllUrlsService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;

import java.util.List;
import java.util.Map;

/**
 * 职责全量url
 *
 * @author 秦学刚
 * @date 2020-07-10 15:25:44
 */
@Controller
@RequestMapping("/link/dutyAllUrls")
public class DutyAllUrlsController extends BasicController<DutyAllUrls> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private DutyAllUrlsService dutyAllUrlsService;

    @Override
    public BasicService<DutyAllUrls> getBasicService() throws Exception {
        return dutyAllUrlsService;
    }

    /**
     * 查询前处理
     *
     * @author qinxg
     * @date 2020/7/10
     * @param entity
     * @param request
     * @return
     **/
    @Override
    public void beforQueryAllOrExample(DutyAllUrls entity, HttpServletRequest request) throws Exception {
        if (entity.getQps() == null || entity.getQps().getFilters() == null) {
            throw new Exception("请选择一条职责");
        }
        JSONObject json = new JSONObject();
        List<Filter> filterList = entity.getQps().getFilters();
        for (Filter filter : filterList) {
            json.put(filter.getProperty(), filter.getValue());
        }
        DutyAllUrls entity1 = json2Object(json, entity);
        BeanUtils.copyProperties(entity1, entity);
    }


    public static <T> T json2Object(JSONObject json, T tClazz) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(tClazz));
        for (Map.Entry<String, Object> entry : json.entrySet()) {
            jsonObject.put(entry.getKey(), entry.getValue());
        }
        return JSONObject.parseObject(jsonObject.toJSONString(), (Class<T>) tClazz.getClass());
    }

}